package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketFeedback;
import com.cskaoyan.market.db.domain.MarketFeedbackExample;
import com.cskaoyan.market.db.mapper.MarketFeedbackMapper;
import com.cskaoyan.market.service.AdminFeedBackService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/24
 * Time: 21:44
 * Description:
 */
public class AdminFeedBackServiceImpl implements AdminFeedBackService {
    @Override
    public List<MarketFeedback> list(String page, String limit, String username, String sort, String order, String id) {
        // 1. 获取sqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketFeedbackMapper mapper = session.getMapper(MarketFeedbackMapper.class);
        // 2.增加筛选条件
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        // 当username不为空时查询
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameEqualTo(username);
        }
        // 当id不为空时查询
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        // 3. 增加排序规则
        marketFeedbackExample.setOrderByClause(sort + " " + order);
        // 4. 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketFeedback> marketFeedbacks = mapper.selectByExample(marketFeedbackExample);
        // 5. 提交事务
        session.commit();
        session.close();
        // 6. 返回list给Controller
        return marketFeedbacks;
    }
}
